using System;

namespace TTEngine.TTMath
{
    public struct SizeRect
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public int X { get; set; }
        public int Y { get; set; }

        public SizeRect(int width, int height, int x = 0, int y = 0)
        {

            Width = width;
            Height = height;
            X = x;
            Y = y;
        }

        public bool Intersects(SizeRect other)
        {
            return X < other.X + other.Width &&
                   X + Width > other.X &&
                   Y < other.Y + other.Height &&
                   Y + Height > other.Y;
        }

        public bool Contains(SizeRect other)
        {
            return X <= other.X &&
                   Y <= other.Y &&
                   X + Width >= other.X + other.Width &&
                   Y + Height >= other.Y + other.Height;
        }

        public int X2 => X + Width;
        public int Y2 => Y + Height;

        public static SizeRect Intersect(SizeRect a, SizeRect b)
        {
            int x1 = System.Math.Max(a.X, b.X);
            int y1 = System.Math.Max(a.Y, b.Y);
            int x2 = System.Math.Min(a.X + a.Width, b.X + b.Width);
            int y2 = System.Math.Min(a.Y + a.Height, b.Y + b.Height);

            if (x2 < x1 || y2 < y1)
            {
                return new SizeRect(0, 0, 0, 0);
            }

            return new SizeRect(x2 - x1, y2 - y1, x1, y1);
        }

        public SizeRect Intersect(SizeRect other)
        {
            return Intersect(this, other);
        }
    }
}